/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package samoloty.view;

import Utils.MyOwnFocusTraversalPolicy;
import Utils.TextFieldFilter;
import Utils.TextFieldFilterDays;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.FocusTraversalPolicy;
import java.awt.Font;
import java.awt.Point;
import java.util.Calendar;
import java.util.GregorianCalendar;
import java.util.Locale;
import javax.swing.DefaultListCellRenderer;
import javax.swing.JLabel;
import javax.swing.ListCellRenderer;
import javax.swing.SwingConstants;
import java.awt.event.FocusListener;
import java.util.Vector;

/**
 *
 * @author borysfan
 */
public class FlightPanel extends ContentPanel implements IContentPanel {

    private int identity = 0;
    private MyLabel fltnoLab;
    private MyLabel origAptLab;
    private MyLabel destAptLab;
    private MyLabel dayLab;
    private MyLabel monthLab;
    private MyLabel timeLab;
    private MyLabel prepareLab;
    private MyLabel yearLab;
    private MyTextField fltnoText;
    private MyTextField origAptText;
    private MyTextField destAppText;
    private MyTextField dayText;
    private MyComboBox monthCombo;
    private MyTextField yearText;
    private MyTextField timeText;
    private MyTextField prepareText;
    private FocusTraversalPolicy travelsalPolicy;

    public FlightPanel() {
        super();
        initPanel();
    }

    private void initPanel() {
        fltnoLab = new MyLabel("FLT NO.", MyLabel.BOLD);

        origAptLab = new MyLabel("ORIG.APT", MyLabel.BOLD);
        destAptLab = new MyLabel("DEST.APT", MyLabel.BOLD);
        dayLab = new MyLabel("DAY", MyLabel.BOLD);
        dayLab.setPreferredSize(new Dimension(dayLab.getPreferredSize().width + 5, dayLab.getPreferredSize().height));
        monthLab = new MyLabel("MONTH", MyLabel.BOLD);
        yearLab = new MyLabel("YEAR", MyLabel.BOLD);
        timeLab = new MyLabel("TIME", MyLabel.BOLD);
        prepareLab = new MyLabel("PREPARED BY", MyLabel.BOLD);
        prepareLab.setPreferredSize(new Dimension(prepareLab.getPreferredSize().width + 5, prepareLab.getPreferredSize().height));

        fltnoLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        origAptLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        destAptLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        dayLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        monthLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        yearLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        timeLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);
        prepareLab.setAlignmentX(MyLabel.RIGHT_ALIGNMENT);

        fltnoText = new MyTextField(10);
        fltnoText.setFieldFilter(new TextFieldFilter(TextFieldFilter.ALPHA_NUMERIC + "/", 8, true));
        fltnoText.setText("LO");

        origAptText = new MyTextField(10);
        origAptText.setFieldFilter(new TextFieldFilter(TextFieldFilter.UPPERCASE, 3, true));

        destAppText = new MyTextField(10);
        destAppText.setFieldFilter(new TextFieldFilter(TextFieldFilter.UPPERCASE, 3, true));


        dayText = new MyTextField(10);
        dayText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 2, false, true));

        yearText = new MyTextField(10);
        yearText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 4, false));

        monthCombo = new MyComboBox(new String[]{"JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC"});
        monthCombo.setPreferredSize(new Dimension(dayText.getPreferredSize().width, monthCombo.getPreferredSize().height - 5));
        ListCellRenderer renderer = new DefaultListCellRenderer();
        ((JLabel) renderer).setHorizontalAlignment(SwingConstants.CENTER);
        monthCombo.setRenderer(renderer);
        monthCombo.setFont(new Font("Tahome", Font.BOLD, 16));
        monthCombo.setFocusable(true);

        timeText = new MyTextField(10);
        timeText.setFieldFilter(new TextFieldFilter(TextFieldFilter.NUMERIC, 4, false));

        prepareText = new MyTextField(10);
        prepareText.setFieldFilter(new TextFieldFilter(TextFieldFilter.UPPERCASE, 15, true));

        setLayout(null);

        add(fltnoLab);
        add(origAptLab);
        add(destAptLab);
        add(dayLab);
        add(monthLab);
        add(timeLab);
        add(prepareLab);
        add(yearLab);

        add(fltnoText);
        add(origAptText);
        add(destAppText);
        add(dayText);
        add(monthCombo);
        add(yearText);
        add(timeText);
        add(prepareText);



        fltnoLab.setPositionByRight(new Point(250, 90));
        origAptLab.setPositionByRight(new Point(250, 150));
        destAptLab.setPositionByRight(new Point(250, 180));
        dayLab.setPositionByRight(new Point(250, 240));
        monthLab.setPositionByRight(new Point(250, 270));
        yearLab.setPositionByRight(new Point(250, 300));
        timeLab.setPositionByRight(new Point(250, 330));
        prepareLab.setPositionByRight(new Point(250, 400));

        fltnoText.setPosition(new Point(255, 90));
        origAptText.setPosition(new Point(255, 150));
        destAppText.setPosition(new Point(255,180));
        dayText.setPosition(new Point(255, 240));
        monthCombo.setPosition(new Point(255, 270));
        yearText.setPosition(new Point(255, 300));
        timeText.setPosition(new Point(255, 330));
        prepareText.setPosition(new Point(255, 400));

        initData();
        serOrderList();
    }

    public void serOrderList() {
        Vector<Component> focusOrder = new Vector<Component>();

        focusOrder.add(fltnoText);
        focusOrder.add(origAptText);
        focusOrder.add(destAppText);
        focusOrder.add(dayText);
        focusOrder.add(monthCombo);
        focusOrder.add(yearText);
        focusOrder.add(timeText);
        focusOrder.add(prepareText);

        travelsalPolicy = new MyOwnFocusTraversalPolicy(focusOrder);
        this.setFocusTraversalPolicy(travelsalPolicy);
        this.setFocusTraversalPolicyProvider(true);
        this.setFocusCycleRoot(true);
    }

    public void initData() {
        Calendar calendar = new GregorianCalendar();
        dayText.setText(Integer.toString(calendar.get(Calendar.DAY_OF_MONTH)));
        monthCombo.setSelectedIndex(calendar.get(Calendar.MONTH));
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        String hour_s = "";
        int minute = calendar.get(Calendar.MINUTE);
        String minute_s = "";
        if (hour < 10) {
            hour_s = "0" + String.valueOf(hour);
        } else {
            hour_s = String.valueOf(hour);
        }
        if (minute < 10) {
            minute_s = "0" + String.valueOf(minute);
        } else {
            minute_s = String.valueOf(minute);
        }

        timeText.setText(hour_s + minute_s);
        //System.out.print(hour_s + minute_s);

        yearText.setText(calendar.get(Calendar.YEAR) + "");
    }

    public String getVFlightNumber() {
        return fltnoText.getText();
    }

    public String getVOrigin() {
        return origAptText.getText();
    }

    public String getVDestination() {
        return destAppText.getText();
    }

    public String getVDay() {
        return dayText.getText();
    }

    public String getVMonth() {
        return (String) monthCombo.getSelectedItem();
    }

    public String getVTime() {
        return timeText.getText();
    }

    public String getVPrepareBy() {
        return prepareText.getText();
    }

    public void reset() {
        fltnoText.setText("");
        origAptText.setText("");
        destAppText.setText("");
        dayText.setText("");
        timeText.setText("");
        prepareText.setText("");
        initData();
    }

    public String getVYear() {
        return yearText.getText();
    }

    public void setFirstFieldFocus() {
        fltnoText.requestFocus();
    }

    public void setYearActionListener(FocusListener changeYearListener) {
        yearText.addFocusListener(changeYearListener);
    }

    public void setTimeActionListener(FocusListener changeTimeListener) {
        timeText.addFocusListener(changeTimeListener);
    }

    public void setVyear(String vYear) {
        yearText.setText(vYear);
    }

    public void setVtime(String vTime) {
        timeText.setText(vTime);
    }

    @Override
    public int getIdentity() {
        return identity;
    }

    public void addFocusLostListener_fltnoText(FocusListener listener){
        fltnoText.addFocusListener(listener);
    }

    public void addFocusLostListener_origAptText(FocusListener listener){
        origAptText.addFocusListener(listener);
    }

    public void addFocusLostListener_destAppText(FocusListener listener){
        destAppText.addFocusListener(listener);
    }

    public void addFocusLostListener_prepareText(FocusListener listener){
        prepareText.addFocusListener(listener);
    }
}
